// Licensed to the LF AI & Data foundation under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include <cstddef>
#include <iostream>
#include <memory>
#include <type_traits>
#include <vector>
#include <string>
#include <mutex>
#include <shared_mutex>

#include "log/Log.h"
#include "Types.h"
#include "arrow/api.h"
#include "arrow/array/array_binary.h"
#include "arrow/chunked_array.h"
#include "common/FieldMeta.h"
#include "common/Utils.h"
#include "common/VectorTrait.h"
#include "common/EasyAssert.h"
#include "common/Array.h"
#include "common/VectorArray.h"
#include "knowhere/dataset.h"
#include "common/TypeTraits.h"

namespace milvus {

// 为 milvus::DataType 创建本地类型别名，简化使用
using DataType = milvus::DataType;

// 字段数据基类，定义了所有字段数据类型应实现的接口
class FieldDataBase {
 public:
    // 显式构造函数，防止隐式类型转换
    explicit FieldDataBase(DataType data_type, bool nullable)
        : data_type_(data_type), nullable_(nullable) {
    }
    // 虚析构函数，确保派生类能正确析构
    virtual ~FieldDataBase() = default;

    // 用指定源数据填充字段数据
    // source: 指向 element_count 个 Type 类型元素的指针
    virtual void
    FillFieldData(const void* source, ssize_t element_count) = 0;

    // 用指定源数据和有效性数据填充字段数据
    virtual void
    FillFieldData(const void* field_data,
                  const uint8_t* valid_data,
                  ssize_t element_count,
                  ssize_t offset) = 0;

    // 用 Arrow ChunkedArray 填充字段数据
    virtual void
    FillFieldData(const std::shared_ptr<arrow::ChunkedArray> arrays) = 0;
    // 用 Arrow Array 填充字段数据
    virtual void
    FillFieldData(const std::shared_ptr<arrow::Array> array) = 0;

    // 使用默认值填充字段数据
    virtual void
    FillFieldData(const std::optional<DefaultValueType> default_value,
                  ssize_t element_count) = 0;

    // 返回指向所有行数据的指针
    virtual void*
    Data() = 0;

    // 返回指向有效性数据的指针
    virtual uint8_t*
    ValidData() = 0;

    // 返回指向偏移位置行数据的指针
    virtual const void*
    RawValue(ssize_t offset) const = 0;

    // 返回所有行的序列化字节大小
    virtual int64_t
    Size() const = 0;

    // 返回数据部分的字节大小
    virtual int64_t
    DataSize() const = 0;

    // 返回有效性数据部分的字节大小
    virtual int64_t
    ValidDataSize() const = 0;

    // 返回指定索引行的序列化字节大小
    virtual int64_t
    DataSize(ssize_t index) const = 0;

    // 返回已填充的行数
    virtual size_t
    Length() const = 0;

    // 检查字段数据是否已满
    virtual bool
    IsFull() const = 0;

    // 检查字段数据是否可为空
    virtual bool
    IsNullable() const = 0;

    // 预留指定容量的空间
    virtual void
    Reserve(size_t cap) = 0;

 public:
    // 返回行容量
    virtual int64_t
    get_num_rows() const = 0;

    // 返回每行表示为多少个 Type 元素
    virtual int64_t
    get_dim() const = 0;

    // 获取数据类型
    DataType
    get_data_type() const {
        return data_type_;
    }

    // 获取空值计数
    virtual int64_t
    get_null_count() const = 0;

    // 检查指定偏移位置的数据是否有效
    virtual bool
    is_valid(ssize_t offset) const = 0;

 protected:
    // 数据类型（不可变）
    const DataType data_type_;
    // 是否可为空（不可变）
    const bool nullable_;
};

// 位集字段数据实现类，用于处理位集类型数据
template <typename Type>
class FieldBitsetImpl : public FieldDataBase {
 public:
    // 禁用默认构造函数
    FieldBitsetImpl() = delete;
    // 禁用移动构造函数
    FieldBitsetImpl(FieldBitsetImpl&&) = delete;
    // 禁用拷贝构造函数
    FieldBitsetImpl(const FieldBitsetImpl&) = delete;

    // 禁用移动赋值操作符
    FieldBitsetImpl&
    operator=(FieldBitsetImpl&&) = delete;
    // 禁用拷贝赋值操作符
    FieldBitsetImpl&
    operator=(const FieldBitsetImpl&) = delete;

    // 显式构造函数，使用位图创建位集字段数据
    explicit FieldBitsetImpl(DataType data_type, TargetBitmap&& bitmap)
        : FieldDataBase(data_type, false), length_(bitmap.size()) {
        data_ = std::move(bitmap).into();
        cap_ = data_.size() * sizeof(Type) * 8;
        Assert(cap_ >= length_);
    }

    // FillFieldData 用于与存储读写，
    // 位集在运行时处理中使用，无需实现
    void
    FillFieldData(const void* source, ssize_t element_count) override {
        ThrowInfo(NotImplemented,
                  "FillFieldData(const void* source, ssize_t element_count)"
                  "not implemented for bitset");
    }

    void
    FillFieldData(const void* field_data,
                  const uint8_t* valid_data,
                  ssize_t element_count,
                  ssize_t offset) override {
        ThrowInfo(NotImplemented,
                  "FillFieldData(const void* field_data, "
                  "const uint8_t* valid_data, ssize_t element_count)"
                  "not implemented for bitset");
    }

    void
    FillFieldData(const std::shared_ptr<arrow::Array> array) override {
        ThrowInfo(NotImplemented,
                  "FillFieldData(const std::shared_ptr<arrow::Array>& array) "
                  "not implemented for bitset");
    }

    void
    FillFieldData(const std::shared_ptr<arrow::ChunkedArray> arrays) override {
        ThrowInfo(
            NotImplemented,
            "FillFieldData(const std::shared_ptr<arrow::ChunkedArray>& arrays) "
            "not implemented for bitset");
    }

    void
    FillFieldData(const std::optional<DefaultValueType> default_value,
                  ssize_t element_count) override {
        ThrowInfo(NotImplemented,
                  "FillFieldData(const const std::optional<DefaultValueType> "
                  "default_value, "
                  "ssize_t element_count) not implemented for bitset");
    }

    virtual void
    FillFieldData(const std::shared_ptr<arrow::StringArray>& array) {
        ThrowInfo(NotImplemented,
                  "FillFieldData(const std::shared_ptr<arrow::StringArray>& "
                  "array) not implemented for bitset");
    }

    virtual void
    FillFieldData(const std::shared_ptr<arrow::BinaryArray>& array) {
        ThrowInfo(NotImplemented,
                  "FillFieldData(const std::shared_ptr<arrow::BinaryArray>& "
                  "array) not implemented for bitset");
    }

    // 获取类名
    std::string
    GetName() const {
        return "FieldBitsetImpl";
    }

    // 返回数据指针
    void*
    Data() override {
        return data_.data();
    }

    // 返回有效性数据指针（位集不支持）
    uint8_t*
    ValidData() override {
        ThrowInfo(NotImplemented, "ValidData() not implemented for bitset");
    }

    // 返回指定偏移位置的原始值（位集不支持）
    const void*
    RawValue(ssize_t offset) const override {
        ThrowInfo(NotImplemented,
                  "RawValue(ssize_t offset) not implemented for bitset");
    }

    // 返回总大小
    int64_t
    Size() const override {
        return DataSize() + ValidDataSize();
    }

    // 返回数据部分大小
    int64_t
    DataSize() const override {
        return sizeof(Type) * get_num_rows();
    }

    // 返回指定偏移位置的数据大小
    int64_t
    DataSize(ssize_t offset) const override {
        return sizeof(Type);
    }

    // 返回有效性数据大小（位集无有效性数据）
    int64_t
    ValidDataSize() const override {
        return 0;
    }

    // 返回长度
    size_t
    Length() const override {
        return get_length();
    }

    // 检查是否已满
    bool
    IsFull() const override {
        auto cap_num_rows = get_num_rows();
        auto filled_num_rows = get_length();
        return cap_num_rows == filled_num_rows;
    }

    // 检查是否可为空（位集不可为空）
    bool
    IsNullable() const override {
        return false;
    }

    // 预留容量
    void
    Reserve(size_t cap) override {
        std::lock_guard lck(cap_mutex_);
        AssertInfo(cap % (8 * sizeof(Type)) == 0,
                   "Reverse bitset size must be a multiple of {}",
                   8 * sizeof(Type));
        if (cap > cap_) {
            data_.resize(cap / (8 * sizeof(Type)));
            cap_ = cap;
        }
    }

 public:
    // 获取行数
    int64_t
    get_num_rows() const override {
        std::shared_lock lck(cap_mutex_);
        return cap_;
    }

    // 获取实际长度
    size_t
    get_length() const {
        std::shared_lock lck(length_mutex_);
        return length_;
    }

    // 获取维度（位集维度为1）
    int64_t
    get_dim() const override {
        return 1;
    }

    // 获取空值计数（位集不支持）
    int64_t
    get_null_count() const override {
        ThrowInfo(NotImplemented,
                  "get_null_count() not implemented for bitset");
    }

    // 检查指定偏移位置是否有效（位集不支持）
    bool
    is_valid(ssize_t offset) const override {
        ThrowInfo(NotImplemented,
                  "is_valid(ssize_t offset) not implemented for bitset");
    }

 private:
    // 存储位集数据的向量
    FixedVector<Type> data_{};
    // 数据可存储的容量
    int64_t cap_;
    // 容量互斥锁
    mutable std::shared_mutex cap_mutex_;
    // 数据中实际元素的数量
    size_t length_{};
    // 长度互斥锁
    mutable std::shared_mutex length_mutex_;
};

// 字段数据实现类模板，用于处理各种类型的数据
template <typename Type, bool is_type_entire_row = false>
class FieldDataImpl : public FieldDataBase {
 public:
    // 禁用移动构造函数
    FieldDataImpl(FieldDataImpl&&) = delete;
    // 禁用拷贝构造函数
    FieldDataImpl(const FieldDataImpl&) = delete;

    // 禁用移动赋值操作符
    FieldDataImpl&
    operator=(FieldDataImpl&&) = delete;
    // 禁用拷贝赋值操作符
    FieldDataImpl&
    operator=(const FieldDataImpl&) = delete;

 public:
    // 显式构造函数，创建指定维度、数据类型和可空性的字段数据
    explicit FieldDataImpl(ssize_t dim,
                           DataType data_type,
                           bool nullable,
                           int64_t buffered_num_rows = 0)
        : FieldDataBase(data_type, nullable),
          num_rows_(buffered_num_rows),
          dim_(is_type_entire_row ? 1 : dim) {
        data_.resize(num_rows_ * dim_);
        if (nullable) {
            if (IsVectorDataType(data_type)) {
                ThrowInfo(NotImplemented, "vector type not support null");
            }
            valid_data_.resize((num_rows_ + 7) / 8, 0xFF);
        }
    }

    // 显式构造函数，使用已有数据创建字段数据
    explicit FieldDataImpl(size_t dim,
                           DataType type,
                           bool nullable,
                           FixedVector<Type>&& data)
        : FieldDataBase(type, nullable), dim_(is_type_entire_row ? 1 : dim) {
        AssertInfo(!nullable, "need to fill valid_data when nullable is true");
        data_ = std::move(data);
        Assert(data_.size() % dim == 0);
        num_rows_ = data_.size() / dim;
    }

    // 显式构造函数，使用已有数据和有效性数据创建字段数据
    explicit FieldDataImpl(size_t dim,
                           DataType type,
                           bool nullable,
                           FixedVector<Type>&& data,
                           FixedVector<uint8_t>&& valid_data)
        : FieldDataBase(type, nullable), dim_(is_type_entire_row ? 1 : dim) {
        AssertInfo(nullable,
                   "no need to fill valid_data when nullable is false");
        data_ = std::move(data);
        valid_data_ = std::move(valid_data);
        Assert(data_.size() % dim == 0);
        num_rows_ = data_.size() / dim;
    }

    // 用指定源数据填充字段数据
    void
    FillFieldData(const void* source, ssize_t element_count) override;

    // 用指定源数据和有效性数据填充字段数据
    void
    FillFieldData(const void* field_data,
                  const uint8_t* valid_data,
                  ssize_t element_count,
                  ssize_t offset) override;

    // 用 Arrow ChunkedArray 填充字段数据
    void
    FillFieldData(const std::shared_ptr<arrow::ChunkedArray> arrays) override;

    // 用 Arrow Array 填充字段数据
    void
    FillFieldData(const std::shared_ptr<arrow::Array> array) override;

    // 使用默认值填充字段数据
    void
    FillFieldData(const std::optional<DefaultValueType> default_value,
                  ssize_t element_count) override;

    // 用 Arrow StringArray 填充字段数据（默认不实现）
    virtual void
    FillFieldData(const std::shared_ptr<arrow::StringArray>& array) {
        ThrowInfo(NotImplemented,
                  "FillFieldData(const std::shared_ptr<arrow::StringArray>& "
                  "array) not implemented by default");
    }

    // 用 Arrow BinaryArray 填充字段数据（默认不实现）
    virtual void
    FillFieldData(const std::shared_ptr<arrow::BinaryArray>& array) {
        ThrowInfo(NotImplemented,
                  "FillFieldData(const std::shared_ptr<arrow::BinaryArray>& "
                  "array) not implemented by default");
    }

    // 获取类名
    std::string
    GetName() const {
        return "FieldDataImpl";
    }

    // 返回数据指针
    void*
    Data() override {
        return data_.data();
    }

    // 返回有效性数据指针
    uint8_t*
    ValidData() override {
        return valid_data_.data();
    }

    // 返回指定偏移位置的原始值
    const void*
    RawValue(ssize_t offset) const override {
        AssertInfo(offset < get_num_rows(),
                   "field data subscript out of range");
        AssertInfo(offset < length(),
                   "subscript position don't has valid value");
        return &data_[offset];
    }

    // 返回总大小
    int64_t
    Size() const override {
        return DataSize() + ValidDataSize();
    }

    // 返回数据部分大小
    int64_t
    DataSize() const override {
        return sizeof(Type) * length() * dim_;
    }

    // 返回指定偏移位置的数据大小
    int64_t
    DataSize(ssize_t offset) const override {
        AssertInfo(offset < get_num_rows(),
                   "field data subscript out of range");
        AssertInfo(offset < length(),
                   "subscript position don't has valid value");
        return sizeof(Type) * dim_;
    }

    // 返回有效性数据大小
    int64_t
    ValidDataSize() const override {
        if (nullable_) {
            return sizeof(uint8_t) * (length() + 7) / 8;
        }
        return 0;
    }

    // 返回长度
    size_t
    Length() const override {
        return length_;
    }

    // 检查是否已满
    bool
    IsFull() const override {
        auto buffered_num_rows = get_num_rows();
        auto filled_num_rows = length();
        return buffered_num_rows == filled_num_rows;
    }

    // 检查是否可为空
    bool
    IsNullable() const override {
        return nullable_;
    }

    // 预留容量
    void
    Reserve(size_t cap) override {
        std::lock_guard lck(num_rows_mutex_);
        if (cap > num_rows_) {
            num_rows_ = cap;
            data_.resize(num_rows_ * dim_);
        }
        if (nullable_) {
            valid_data_.resize((num_rows_ + 7) / 8, 0xFF);
        }
    }

 public:
    // 获取行数
    int64_t
    get_num_rows() const override {
        std::shared_lock lck(num_rows_mutex_);
        return num_rows_;
    }

    // 调整字段数据大小
    void
    resize_field_data(int64_t num_rows) {
        std::lock_guard lck(num_rows_mutex_);
        if (num_rows > num_rows_) {
            num_rows_ = num_rows;
            data_.resize(num_rows_ * dim_);
            if (nullable_) {
                valid_data_.resize((num_rows + 7) / 8, 0xFF);
            }
        }
    }

    // 获取实际长度
    size_t
    length() const {
        std::shared_lock lck(tell_mutex_);
        return length_;
    }

    // 获取维度
    int64_t
    get_dim() const override {
        return dim_;
    }

    // 获取空值计数
    int64_t
    get_null_count() const override {
        std::shared_lock lck(tell_mutex_);
        return null_count_;
    }

    // 检查指定偏移位置是否有效
    bool
    is_valid(ssize_t offset) const override {
        std::shared_lock lck(tell_mutex_);
        AssertInfo(offset < get_num_rows(),
                   "field data subscript out of range");
        AssertInfo(offset < length(),
                   "subscript position don't has valid value");
        if (!nullable_) {
            return true;
        }
        auto bit = (valid_data_[offset >> 3] >> ((offset & 0x07))) & 1;
        return bit;
    }

 protected:
    // 存储实际数据的向量
    FixedVector<Type> data_{};
    // 存储有效性数据的向量
    FixedVector<uint8_t> valid_data_{};
    // 数据可容纳的元素数量
    int64_t num_rows_;
    // 行数互斥锁
    mutable std::shared_mutex num_rows_mutex_;
    // 空值计数
    int64_t null_count_{0};
    // 数据中实际元素的数量
    size_t length_{};
    // 长度互斥锁
    mutable std::shared_mutex tell_mutex_;

 private:
    // 维度（每行表示为多少个 Type 元素）
    const ssize_t dim_;
};

// 字符串字段数据实现类，专门处理字符串类型数据
class FieldDataStringImpl : public FieldDataImpl<std::string, true> {
 public:
    // 显式构造函数，创建字符串字段数据
    explicit FieldDataStringImpl(DataType data_type,
                                 bool nullable,
                                 int64_t total_num_rows = 0)
        : FieldDataImpl<std::string, true>(
              1, data_type, nullable, total_num_rows) {
    }

    // 返回数据部分大小
    int64_t
    DataSize() const override {
        int64_t data_size = 0;
        for (size_t offset = 0; offset < length(); ++offset) {
            data_size += data_[offset].size();
        }

        return data_size;
    }

    // 返回指定偏移位置的数据大小
    int64_t
    DataSize(ssize_t offset) const override {
        AssertInfo(offset < get_num_rows(),
                   "field data subscript out of range");
        AssertInfo(offset < length(),
                   "subscript position don't has valid value");
        return data_[offset].size();
    }

    // 用 Arrow StringArray 填充字段数据
    void
    FillFieldData(const std::shared_ptr<arrow::StringArray>& array) override {
        auto n = array->length();
        if (n == 0) {
            return;
        }

        std::lock_guard lck(tell_mutex_);
        if (length_ + n > get_num_rows()) {
            resize_field_data(length_ + n);
        }

        auto i = 0;
        for (const auto& str : *array) {
            data_[length_ + i] = str.value();
            i++;
        }
        if (IsNullable()) {
            auto valid_data = array->null_bitmap_data();
            if (valid_data != nullptr) {
                bitset::detail::ElementWiseBitsetPolicy<uint8_t>::op_copy(
                    valid_data,
                    array->offset(),
                    valid_data_.data(),
                    length_,
                    n);
            }
        }
        length_ += n;
    }
};

// JSON字段数据实现类，专门处理JSON类型数据
class FieldDataJsonImpl : public FieldDataImpl<Json, true> {
 public:
    // 显式构造函数，创建JSON字段数据
    explicit FieldDataJsonImpl(DataType data_type,
                               bool nullable,
                               int64_t total_num_rows = 0)
        : FieldDataImpl<Json, true>(1, data_type, nullable, total_num_rows) {
    }

    // 返回数据部分大小
    int64_t
    DataSize() const override {
        int64_t data_size = 0;
        for (size_t offset = 0; offset < length(); ++offset) {
            data_size += data_[offset].data().size();
        }

        return data_size;
    }

    // 返回指定偏移位置的数据大小
    int64_t
    DataSize(ssize_t offset) const override {
        AssertInfo(offset < get_num_rows(),
                   "field data subscript out of range");
        AssertInfo(offset < length(),
                   "subscript position don't has valid value");
        return data_[offset].data().size();
    }

    // 用 Arrow Array 填充字段数据
    void
    FillFieldData(const std::shared_ptr<arrow::Array> array) override {
        AssertInfo(array->type()->id() == arrow::Type::type::BINARY,
                   "inconsistent data type, expected: {}, got: {}",
                   "BINARY",
                   array->type()->ToString());
        auto json_array = std::dynamic_pointer_cast<arrow::BinaryArray>(array);
        FillFieldData(json_array);
    }

    // 使用默认值填充字段数据（用于生成没有相关binlog的新增字段）
    void
    FillFieldData(const std::optional<DefaultValueType> default_value,
                  ssize_t element_count) override {
        // todo: add json default_value
        AssertInfo(!default_value.has_value(),
                   "json type not support default_value");
        if (element_count == 0) {
            return;
        }
        null_count_ = element_count;

        std::lock_guard lck(tell_mutex_);
        if (length_ + element_count > get_num_rows()) {
            resize_field_data(length_ + element_count);
        }

        bitset::detail::ElementWiseBitsetPolicy<uint8_t>::op_fill(
            valid_data_.data(), length_, element_count, false);
        length_ += element_count;
    }

    // 用 Arrow BinaryArray 填充字段数据
    void
    FillFieldData(const std::shared_ptr<arrow::BinaryArray>& array) override {
        auto n = array->length();
        if (n == 0) {
            return;
        }
        null_count_ = array->null_count();

        std::lock_guard lck(tell_mutex_);
        if (length_ + n > get_num_rows()) {
            resize_field_data(length_ + n);
        }

        auto i = 0;
        for (const auto& json : *array) {
            if (!json.has_value()) {
                i++;
                continue;
            }
            data_[length_ + i++] = Json(simdjson::padded_string(json.value()));
        }
        if (IsNullable()) {
            auto valid_data = array->null_bitmap_data();
            if (valid_data != nullptr) {
                bitset::detail::ElementWiseBitsetPolicy<uint8_t>::op_copy(
                    valid_data,
                    array->offset(),
                    valid_data_.data(),
                    length_,
                    n);
            }
        }
        length_ += n;
    }

    // 仅用于测试，添加JSON数据
    void
    add_json_data(const std::vector<Json>& json) {
        std::lock_guard lck(tell_mutex_);
        if (length_ + json.size() > get_num_rows()) {
            resize_field_data(length_ + json.size());
        }

        for (size_t i = 0; i < json.size(); ++i) {
            data_[length_ + i] = json[i];
        }
        length_ += json.size();
    }
};

// 稀疏向量字段数据实现类，专门处理稀疏向量类型数据
class FieldDataSparseVectorImpl
    : public FieldDataImpl<knowhere::sparse::SparseRow<float>, true> {
 public:
    // 显式构造函数，创建稀疏向量字段数据
    explicit FieldDataSparseVectorImpl(DataType data_type,
                                       int64_t total_num_rows = 0)
        : FieldDataImpl<knowhere::sparse::SparseRow<float>, true>(
              /*dim=*/1, data_type, false, total_num_rows),
          vec_dim_(0) {
        AssertInfo(data_type == DataType::VECTOR_SPARSE_FLOAT,
                   "invalid data type for sparse vector");
    }

    // 返回数据部分大小
    int64_t
    DataSize() const override {
        int64_t data_size = 0;
        for (size_t i = 0; i < length(); ++i) {
            data_size += data_[i].data_byte_size();
        }
        return data_size;
    }

    // 返回指定偏移位置的数据大小
    int64_t
    DataSize(ssize_t offset) const override {
        AssertInfo(offset < get_num_rows(),
                   "field data subscript out of range");
        AssertInfo(offset < length(),
                   "subscript position don't has valid value");
        return data_[offset].data_byte_size();
    }

    // 用指定源数据填充字段数据
    // source: 指向 element_count 个 knowhere::sparse::SparseRow<float> 类型元素的指针
    void
    FillFieldData(const void* source, ssize_t element_count) override {
        if (element_count == 0) {
            return;
        }

        std::lock_guard lck(tell_mutex_);
        if (length_ + element_count > get_num_rows()) {
            resize_field_data(length_ + element_count);
        }
        auto ptr =
            static_cast<const knowhere::sparse::SparseRow<float>*>(source);
        for (int64_t i = 0; i < element_count; ++i) {
            auto& row = ptr[i];
            vec_dim_ = std::max(vec_dim_, row.dim());
        }
        std::copy_n(ptr, element_count, data_.data() + length_);
        length_ += element_count;
    }

    // 用 Arrow BinaryArray 填充字段数据
    // array 中的每个二进制数据都是一个 knowhere::sparse::SparseRow<float>
    void
    FillFieldData(const std::shared_ptr<arrow::BinaryArray>& array) override {
        auto n = array->length();
        if (n == 0) {
            return;
        }

        std::lock_guard lck(tell_mutex_);
        if (length_ + n > get_num_rows()) {
            resize_field_data(length_ + n);
        }

        for (int64_t i = 0; i < array->length(); ++i) {
            auto view = array->GetView(i);
            auto& row = data_[length_ + i];
            row = CopyAndWrapSparseRow(view.data(), view.size());
            vec_dim_ = std::max(vec_dim_, row.dim());
        }
        length_ += n;
    }

    // 获取向量维度
    int64_t
    Dim() const {
        return vec_dim_;
    }

 private:
    // 向量维度
    int64_t vec_dim_ = 0;
};

// 数组字段数据实现类，专门处理数组类型数据
class FieldDataArrayImpl : public FieldDataImpl<Array, true> {
 public:
    // 显式构造函数，创建数组字段数据
    explicit FieldDataArrayImpl(DataType data_type,
                                bool nullable,
                                int64_t total_num_rows = 0)
        : FieldDataImpl<Array, true>(1, data_type, nullable, total_num_rows) {
    }

    // 返回数据部分大小
    int64_t
    DataSize() const override {
        int64_t data_size = 0;
        for (size_t offset = 0; offset < length(); ++offset) {
            data_size += data_[offset].byte_size();
        }
        return data_size;
    }

    // 返回指定偏移位置的数据大小
    int64_t
    DataSize(ssize_t offset) const override {
        AssertInfo(offset < get_num_rows(),
                   "field data subscript out of range");
        AssertInfo(offset < length(),
                   "subscript position don't has valid value");
        return data_[offset].byte_size();
    }
};

// 向量数组字段数据实现类，专门处理向量数组类型数据
// is_type_entire_row 设置为 true，因为 data_ 中的每个元素都是一个 VectorArray
class FieldDataVectorArrayImpl : public FieldDataImpl<VectorArray, true> {
 public:
    // 显式构造函数，创建向量数组字段数据
    explicit FieldDataVectorArrayImpl(DataType data_type,
                                      int64_t total_num_rows = 0)
        : FieldDataImpl<VectorArray, true>(
              1, data_type, false, total_num_rows) {
    }

    // 返回数据部分大小
    int64_t
    DataSize() const override {
        int64_t data_size = 0;
        for (size_t offset = 0; offset < length(); ++offset) {
            data_size += data_[offset].byte_size();
        }
        return data_size;
    }

    // 返回指定偏移位置的数据大小
    int64_t
    DataSize(ssize_t offset) const override {
        AssertInfo(offset < get_num_rows(),
                   "field data subscript out of range");
        AssertInfo(offset < length(),
                   "subscript position don't has valid value");
        return data_[offset].byte_size();
    }
};

}  // namespace milvus